#include <iostream>
#include <memory>
#include <pthread.h>
#include "ringbuffer/ringbuffer.hpp"


#define NUM_THREADS 2
struct thread_data{
   int  thread_id;
   char *message;
};

ringbuffer::Ringbuffer<char*, 256> message;
char message_info[1024];

void* systick_handler(void* args)
{
  struct thread_data *data;
  data = (struct thread_data *) args;
  for(int i = 0; i < 3; i++){
    sprintf(message_info, "id: %d, %s", data->thread_id, data->message);
    message.insert(message_info);
  }
  return 0;
}



int ringbuffer_sample(int, char**) {
  pthread_t threads[NUM_THREADS];
  struct thread_data td[NUM_THREADS];
  for(int i = 0; i < NUM_THREADS; ++i)
  {
      td[i].thread_id = i;
      td[i].message = (char*)"This is message";
      int ret = pthread_create(&threads[i], NULL, systick_handler, (void *)&td[i]);
      if (ret != 0)
      {
          std::cout << "pthread_create error: error_code=" << ret << std::endl;
      }
  }

  while(1)
  {
    char* tmp = nullptr;
    while(!message.remove(tmp));
    printf("%s fired\n", tmp);
  }
  pthread_exit(NULL);
  std::cout << "Hello, world!\n";
}
